// pages/operating_password/operating_password.js
let App = getApp();
var countdown = 60;
var settime = function (that) {
    if (countdown == 0) {
        that.setData({
            is_show: true
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })

        countdown--;
    }
    setTimeout(function () {
        settime(that)
    }, 1000)
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        is_show: true, //计时器开关
        phone:'',//手机号
        code:'',//验证码
        password_o:"",//密码一
        password_t:"",//密码二
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            phone: wx.getStorageSync('phone')
        })

    },
    //int输入框
    phoneInputBlur: function (e) {
        var phoneName = e.detail.value
        console.log(e);
        this.setData({
            phone: phoneName
        })
        // wx.setStorageSync(key, data)
    },
    //获取验证码
    codeInputBlur: function (e) {
        var codes = e.detail.value;
        this.setData({
            code: codes
        })
    },
    // 获取验证码
    clickVerify: function (e) {
        var This = this;
        if (This.data.phone == ''){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000,
            })
            return false;
        } else if (!(/^1[3456789]\d{9}$/.test(This.data.phone))){
            wx.showToast({
                title: '手机号格式不对',
                icon: 'none',
                duration: 2000,
            })
            return false;
        }
        var phone = This.data.phone;
        App._get('coach/Agreement/send_phone', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            mobile: phone
        }, function (result) {
            if (result.status == "1") {
                wx.showToast({
                    title: '发送成功',
                    icon: 'success',
                    duration: 2000,
                })
            } else {
                wx.showToast({
                    title: '手机号错误',
                    icon: 'none',
                    duration: 2000,
                })
            }
        })
        var This = this
        This.setData({
            is_show: (!This.data.is_show)
        })
        countdown = 60;
        settime(This);
    },
    //密码one
    passwordONE:function(e){
        var password = e.detail.value;
        this.setData({
            password_o: password
        })
    },
    //密码two
    passwordTWO: function (e) {
        var password = e.detail.value;
        this.setData({
            password_t: password
        })
    },
    //
    btn_w:function(e){
        var This = this;
        if(This.data.phone == ''){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000,
            })
            return false;
        }else if(This.data.code == ''){
            wx.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 2000,
            })
            return false;
        }else if(This.data.password_o == ''){
            wx.showToast({
                title: '请输入密码',
                icon: 'none',
                duration: 2000,
            })
            return false;
        } else if (This.data.password_t == ''){
            wx.showToast({
                title: '请输再次入密码',
                icon: 'none',
                duration: 2000,
            })
            return false;
        } else if (This.data.password_t != This.data.password_o){
            wx.showToast({
                title: '两个密码不一致请重新设置',
                icon: 'none',
                duration: 2000,
            })
            return false;
        }
        App._get('coach/Agreement/set_password',{
            mobile: This.data.phone,
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            password: This.data.password_o,
            confirm_password: This.data.password_t,
            code: This.data.code
        },function(res){
            console.log(res)
            if(res.status == 1){
                wx.showToast({
                    title: res.message,
                    icon: 'success',
                    duration: 2000,
                })
                wx.switchTab({
                    url: '../user/user',
                })
            }else{
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000,
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})